con


  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


  BUFFSIZE      = 48

  LED           = 16
  XBRX          = 2
  XBTX          = 1
  VFD_PIN       = 0


var

  byte  buff[BUFFSIZE]

  
obj

  xb:   "FullDuplexSerial"
  vfd:  "FullDuplexSerial"
  

pub Start | i

  xb.start(XBRX, XBTX, 0, 9600)
  vfd.start(-1, VFD_PIN, 0, 38_400)

  dira[LED]~~

  repeat i from 0 to BUFFSIZE - 1
    buff[i] := 0                'zero the buffer
  
  go



pub go | x

  'get xbee module info and print to LCD

  'get xbee node identifier
  enter_command

  xb.str(STRING("ATNI", $0D))   'send command to get node identifier from XBee
  read_to_buff                  'get response from xbee 
  vfd.str(STRING($0C, "NI="))
  vfd.str(@buff)                'print it

  'get PAN ID that the xbee is confugred to establish/join
  xb.str(STRING("ATOP", $0D))   'send command to get the operating pan id from XBee
  read_to_buff                  'get response from xbee
  vfd.str(STRING($0A, $0D, "OP_PAN_ID="))
  vfd.str(@buff)                'print it

  pause(2_000_000)

  


  outa[LED]~~ 
  outa[LED]~
  

  repeat


  
pub read_to_buff | i, x

  i := x := 0
  repeat until x == $0D
    x := xb.rx
    buff[i++] := x

  buff[i-1] := 0                  'null terminate for strings, write over trailing CR


pub enter_command | i, tmp

  
  pause(1_000_000)
  repeat 3
    xb.tx($2B)

  repeat 2
    xb.rx
  if xb.rx <> $0D
    vfd.str(STRING($0c, "failed cmd mode"))
    abort -1

  return 0
  
  



pri Pause(duration)

  'This function pauses program execution for approximately [duration] micro seconds
  'so 1_000_000 would be approximately one second.  Doesnt account for instruction
  'execution time overhead.

  if duration < 381
    duration := 381             'lower bound limit. anything lower than this, we
                                'have to wait until the counter comes back around,
                                'which is MUCH longer than [duration].
  waitcnt(((clkfreq / 1_000_000) * duration) + cnt)


pri Future_cnt(usec)            'simple method to return the count for some usec in the future
  return(((clkfreq / 1_000_000) * usec) + cnt) 
 


dat

         